<template>
  <BasicTable @register="registerTable" @resize-column="handleResizeColumn">
    <template #toolbar>
      <a-button type="primary" @click="handleCreate()"> 新增 </a-button>
    </template>
    <template #moduleName="{ record, column }">
      <div @click="handleManager(record)" class="cursor-pointer text-blue-800">{{
        record.moduleName
      }}</div>
    </template>
    <template #action="{ record }">
      <TableAction
        :actions="[
          {
            icon: 'clarity:note-edit-line',
            onClick: handleEdit.bind(null, record),
          },
          {
            icon: 'ant-design:delete-outlined',
            color: 'error',
            popConfirm: {
              title: '是否确认删除',
              placement: 'leftBottom',
              confirm: handleDelete.bind(null, record),
            },
          },
        ]"
      />
    </template>
  </BasicTable>
  <RelateComponentDrawer @register="registerDrawer" @success="handleSuccess" />
</template>
<script lang="ts">
  import { defineComponent, onMounted } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';
  import { useDrawer } from '/@/components/Drawer';
  import RelateComponentDrawer from './RelateComponentDrawer.vue';
  import { columns, searchFormSchema } from './relateComponent.data';
  import { getVisualListRelateModule, getVisualDelRelateModule } from '/@/api/module/module';
  import { useGo } from '/@/hooks/web/usePage';

  export default defineComponent({
    name: 'RelateComponent',
    components: { BasicTable, RelateComponentDrawer, TableAction },
    props: {
      formCode: {
        type: String,
        default: '',
      },
      moduleCode: {
        type: String,
        default: '',
      },
    },
    setup(props) {
      const [registerDrawer, { openDrawer }] = useDrawer();

      const [registerTable, { reload, getDataSource }] = useTable({
        title: '',
        api: getVisualListRelateModule,
        columns,
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        // searchInfo: {
        //   op: 'search',
        // }, //额外的参数
        useSearchForm: false,
        showTableSetting: true,
        bordered: true,
        indexColumnProps: { width: 50 },
        showIndexColumn: true,
        canResize: false,
        pagination: false, //无分页
        beforeFetch: (info) => {
          return {
            ...info,
            // moduleCode: props.moduleCode,
            formCode: props.formCode,
          };
        },
        actionColumn: {
          width: 60,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right',
        },
      });

      function handleCreate() {
        openDrawer(true, {
          isUpdate: false,
          formCode: props.formCode,
          moduleCode: props.moduleCode,
          record: {
            order: getDataSource().length + 1,
          },
        });
      }

      function handleEdit(record: Recordable) {
        openDrawer(true, {
          record,
          isUpdate: true,
          formCode: props.formCode,
          moduleCode: props.moduleCode,
        });
      }

      async function handleDelete(record: Recordable) {
        try {
          await getVisualDelRelateModule({
            formCode: props.formCode,
            relateCode: record.moduleCode,
          });
          handleSuccess();
        } catch (error) {
          console.error(error);
        }
      }

      function handleSuccess() {
        reload();
      }

      const go = useGo();

      function handleManager(record) {
        go({
          path: '/managerPageSetup',
          query: {
            moduleCode: record.moduleCode,
            formCode: record.moduleFormCode,
            title: record.moduleName,
            cacheName: `managerPageSetup${record.moduleCode}`,
          },
        });
      }

      onMounted(() => {});
      return {
        registerTable,
        registerDrawer,
        handleCreate,
        handleEdit,
        handleDelete,
        handleSuccess,
        handleResizeColumn: (w, col) => {
          col.width = w;
        },
        handleManager,
      };
    },
  });
</script>
